﻿namespace Blaze.Framework.LuaExtensions
{
    using System;
    using UnityEngine;

    /// <summary>
    /// Lua字段信息。
    /// </summary>
    [Serializable]
    public struct LuaFieldPair
    {
        /// <summary>
        /// 在Lua中获取引用时的键。
        /// </summary>
        public string K;

        /// <summary>
        /// 在Lua中获取引用时所对应的游戏对象或资源。
        /// </summary>
        public UnityEngine.Object V;

        /// <summary>
        /// 在Lua中获取文本。
        /// </summary>
        public string V1;

        /// <summary>
        /// 在Lua中获取数字。
        /// </summary>
        public float V2;

        /// <summary>
        /// 字段类型，根据类型决定这个字段使用的哪个变量。
        /// </summary>
        public LuaFieldType T;

        /// <summary>
        /// 清理所有的值。
        /// </summary>
        public void ClearValues()
        {
            V = null;
            V1 = null;
            V2 = 0;
        }

#if UNITY_EDITOR

        /// <summary>
        /// 尝试将Lua字段信息的类型变更为新类型。
        /// </summary>
        /// <param name="newType">新类型</param>
        /// <returns>是否变更成功</returns>
        public bool TryConvertTo(LuaFieldType newType)
        {
            if (T == newType)
                return true;

            if (T == LuaFieldType.GameObject && newType == LuaFieldType.Component)
            {
                var go = V as GameObject;
                if (go == null)
                    return false;
                V = go.GetComponent<Component>();
                T = newType;
                return true;
            }

            if (T == LuaFieldType.GameObject && newType == LuaFieldType.LuaTable)
            {
                var go = V as GameObject;
                if (go == null)
                    return false;
                V = go.GetComponent<LuaBehaviour>();
                T = newType;
                return true;
            }

            if (T == LuaFieldType.Component && newType == LuaFieldType.GameObject)
            {
                var c = V as Component;
                if (c == null || c.gameObject == null)
                    return false;
                V = c.gameObject;
                T = newType;
                return true;
            }

            if (T == LuaFieldType.Component && newType == LuaFieldType.LuaTable)
            {
                var c = V as LuaBehaviour;
                if (c == null || c.gameObject == null)
                    return false;
                V = c;
                T = newType;
                return true;
            }

            if (T == LuaFieldType.LuaTable && newType == LuaFieldType.Component)
            {
                var c = V as Component;
                if (c == null || c.gameObject == null)
                    return false;
                V = c;
                T = newType;
                return true;
            }

            if (T == LuaFieldType.LuaTable && newType == LuaFieldType.GameObject)
            {
                var c = V as LuaBehaviour;
                if (c == null || c.gameObject == null)
                    return false;
                V = c.gameObject;
                T = newType;
                return true;
            }

            return false;
        }

#endif
    }
}